/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

/**
 *
 * @author User
 */


import engine.Singleton;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import json.ClientProtocol;
import org.json.simple.parser.ParseException;
import tictactoe.SmallField;
import webClient.HttpClient.SocketProcessor;

public class WebConnectClient extends Thread {
  private int port;
  private String login;
  private String addr;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
   private String partnerName;
   public javax.swing.JPanel FieldPannel;
   
   public SocketProcessor sok; //webclientprotocol вызывает эту переменную и может выполнять метод  sok.writeResponse(WebPages.listOfPlaer());
 
  
  public WebConnectClient(int port, String login, String addr, SocketProcessor sok) throws IOException{
    this.port = port;
    this.login = login;
    this.addr = addr;
    this.sok = sok;
    addr = "127.0.0.1"; // это закоментить
    port = 8888;
    
    System.out.println("addr = " + addr);
    socket = new Socket(addr, port);
    System.out.println("socket = " + socket);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    
    start();
    
  }
  
  public void setSok(SocketProcessor sok)
  {
    this.sok = sok;
  }
  
  public void disconnect(){
     out.println("{\"Type\": \"End\"}");
  }
  
  public void setPlaer(String plaer){
     out.println("{\"Type\":\"SetPartner\", \"Name\":\""+ plaer +"\", \"OfferName\":\""+ login +"\"}");
  }
  
  
  public void refuse(String send){
     out.println(send);
  }
  
  public void setPartner(String name){
     partnerName = name;
  }
  
  public String getLogin()
  {
    return login;
  }
  
  public String getPartnerName()
  {
    return partnerName;
  }
  
  public void run()
 // public static void connect(int port, String login, String addr) throws IOException, ParseException
  {

   //WebClientProtocol cl = new WebClientProtocol(this);
    
    try {

      out.println("{\"Type\":\"Auth\", \"Name\":\"" + login + "\"}");
      
      while (true)
            {
                String str = null;
        try {
          str = in.readLine();
        } catch (IOException ex) {
          Logger.getLogger(WebConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
                if (str != null){
                  if (str.equals("{\"Type\": \"End\"}")) {
                    out.println("{\"Type\": \"End\"}");
                    break;
                  }
                }
        try {
          SocketTowebClient.data(str);
        } catch (MalformedURLException ex) {
          Logger.getLogger(WebConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
          Logger.getLogger(WebConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        
//          try {
//            cl.getClient(str);
//          } catch (Throwable ex) {
//            Logger.getLogger(WebConnectClient.class.getName()).log(Level.SEVERE, null, ex);
//          }
      
                System.out.println("Client comming string: " + str);
              //    out.println(Singleton.outClient);
                
            }
      
    } finally {
      System.out.println("closing ConnectClient ...");
      try {
        socket.close();
      } catch (IOException ex) {
        Logger.getLogger(WebConnectClient.class.getName()).log(Level.SEVERE, null, ex);
      }
   
    }
    
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
  }
  
}